package com.oujia.cloud.cloudcommon.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

public class ChartUtil {
    /**
     * 传入map  生成头为XML的xml字符串，例：<xml><key>123</key></xml>
     *
     * @param reqMap
     * @return
     */
    public static String creatXml(Map<String, String> reqMap) {
        Set<String> set = reqMap.keySet();
        StringBuffer b = new StringBuffer();
        b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        b.append("<xml>");
        for (String key : set) {
            b.append("<" + key + ">").append(reqMap.get(key)).append("</" + key + ">");
        }
        b.append("</xml>");
        return b.toString();
    }

    /**
     * 得到加密值
     *
     * @param map
     * @return
     */
    public static String getSign(Map<String, String> map, String appSecret) {
        String[] keys = map.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        StringBuffer reqStr = new StringBuffer();
        for (String key : keys) {
            String v = map.get(key);
            if (v != null && !v.equals("")) {
                reqStr.append(key).append("=").append(v).append("&");
            }
        }


        reqStr.append("key").append("=").append(appSecret);

        return WeiMd5.encode(reqStr.toString()).toUpperCase();
    }
    /**
     * 得到本地机器的IP
     *
     * @return
     */
    public static String getHostIp() {
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }

    /**
     * 得到随机3位数字
     *
     * @return
     */
    public static String getRandomString() {
        int length = 3;
        Random random = new Random();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; ++i) {
            sb.append(random.nextInt(9) + 1);
        }
        return sb.toString();
    }


    // /**
    //  * 得到随机字符串
    //  *
    //  * @return
    //  */
    // public static String getRandomString() {
    //     int length = 32;
    //     String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //     Random random = new Random();
    //     StringBuffer sb = new StringBuffer();
    //
    //     for (int i = 0; i < length; ++i) {
    //         int number = random.nextInt(62);//[0,62)
    //         sb.append(str.charAt(number));
    //     }
    //     return sb.toString();
    // }

    /**
     * 获取时间戳
     * 时间戳从1970年1月1日00:00:00至今的秒数
     */
    public static long getTimeStamp() {
        Date d = new Date();
        long timeStamp = d.getTime();     //getTime()得到的是毫秒
        // long timeStamp = d.getTime()/1000;     //getTime()得到的是毫秒 得到秒
        return timeStamp;
    }


}
